﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Utils.Data;
using UniMana.Data;


namespace UniMana.Win.Systems
{
    public partial class FrmCreateRule : DevExpress.XtraEditors.XtraForm
    {
        System_RuleInfo m_sri;
        public delegate void PassData(object[] _sri);
        public PassData passData;
        int curentGroupKey = -1;
        public FrmCreateRule(System_RuleInfo _sri=null)
        {
            InitializeComponent();
            if (_sri != null)
            {
                m_sri = _sri;
            }
          
            LoadData();
            if (lueSystem_Group.EditValue != null)
            {
                curentGroupKey = int.Parse(lueSystem_Group.EditValue.ToString());
            }
        }

        public void LoadData()
        {
            lueSystem_Group.GetData(System_GroupData.GetList(new DBMain()),"System_GroupKey","Name");
            if (m_sri != null)
            {
                lueSystem_Group.EditValue = int.Parse(m_sri.Properties.ParentID);
                txtRuleID.Text = m_sri.Properties.RuleID;
                txtRuleName.Text = m_sri.Properties.RuleName;
                txtRuleNameVi.Text = m_sri.Properties.RuleNameVi;
                moeDescription.Text = m_sri.Properties.Description;

                System_Group_RuleInfo sgri = new System_Group_RuleInfo(int.Parse(lueSystem_Group.EditValue.ToString()), m_sri.Properties.System_RuleKey);
                this.ckeAccess.Checked = sgri.Properties.AllowAccess.Value;
                this.ckeAdd.Checked = sgri.Properties.AllowAdd.Value;
                this.ckeDelete.Checked = sgri.Properties.AllowDelete.Value;
                this.ckeEdit.Checked = sgri.Properties.AllowEdit.Value;
                this.ckeExportExcel.Checked = sgri.Properties.AllowExport.Value;
                this.ckeImportExcel.Checked = sgri.Properties.AllowImport.Value;
                this.ckePrint.Checked = sgri.Properties.AllowPrint.Value;
                this.ckeSave.Checked = sgri.Properties.AllowSave.Value;

            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            
            if (txtRuleID.Text == "")
            {
                MessageBox.Show("Mã luật không được rỗng");
                return;
            }

            if (txtRuleNameVi.Text == "")
            {
                MessageBox.Show("Tên luật không được rỗng");
                return;
            }
            
            if (m_sri == null)
            {
                if(System_RuleData.CheckExist(txtRuleID.Text))
                {
                    MessageBox.Show("Luật đã tồn tại trong hệ thống. Vui lòng kiểm tra lại");
                    return;
                }

                m_sri = new System_RuleInfo(txtRuleID.Text);
            }
            else
            {
                m_sri.Properties.RuleID = txtRuleID.Text;
            }

            if (lueSystem_Group.EditValue == null)
            {
                MessageBox.Show("Vui lòng chọn nhóm");
                return;
            }

            if (curentGroupKey!=-1&&curentGroupKey != int.Parse(lueSystem_Group.EditValue.ToString()))
            {
                System_Group_RuleInfo sgriRemove = new System_Group_RuleInfo(curentGroupKey, m_sri.Properties.System_RuleKey);
                sgriRemove.Delete();
                sgriRemove.Save();
                DataTable dtRemove = System_UserData.GetAllUserByGroup(curentGroupKey).ToDataTable();
                foreach (DataRow dr in dtRemove.Rows)
                {
                    System_User_RuleInfo suri = new System_User_RuleInfo(Guid.Parse(dr["System_UserKey"].ToString()), m_sri.Properties.System_RuleKey);

                    suri.Delete();
                    suri.Save();
                }
            }

            System_Group_RuleInfo sgri = new System_Group_RuleInfo(int.Parse(lueSystem_Group.EditValue.ToString()), m_sri.Properties.System_RuleKey);
            sgri.Properties.AllowAccess = ckeAccess.Checked;
            sgri.Properties.AllowAdd = ckeAdd.Checked;
            sgri.Properties.AllowDelete = ckeDelete.Checked;
            sgri.Properties.AllowEdit = ckeEdit.Checked;
            sgri.Properties.AllowExport = ckeExportExcel.Checked;
            sgri.Properties.AllowImport = ckeImportExcel.Checked;
            sgri.Properties.AllowPrint = ckePrint.Checked;
            sgri.Properties.AllowSave = ckeSave.Checked;

            m_sri.Properties.RuleName = txtRuleName.Text;
            m_sri.Properties.RuleNameVi = txtRuleNameVi.Text;
            m_sri.Properties.Description = moeDescription.Text;

            m_sri.Properties.ParentID = lueSystem_Group.EditValue.ToString();
           

            DataTable dtGr = System_UserData.GetAllUserByGroup(sgri.Properties.System_GroupKey).ToDataTable();
            foreach(DataRow dr in dtGr.Rows)
            {
                System_User_RuleInfo suri = new System_User_RuleInfo(Guid.Parse(dr["System_UserKey"].ToString()), m_sri.Properties.System_RuleKey);
                suri.Properties.AllowAccess = ckeAccess.Checked;
                suri.Properties.AllowAdd = ckeAdd.Checked;
                suri.Properties.AllowDelete = ckeDelete.Checked;
                suri.Properties.AllowEdit = ckeEdit.Checked;
                suri.Properties.AllowExport = ckeExportExcel.Checked;
                suri.Properties.AllowImport = ckeImportExcel.Checked;
                suri.Properties.AllowPrint = ckePrint.Checked;
                suri.Properties.AllowSave = ckeSave.Checked;

                suri.Save();
            }

            m_sri.Properties.Level = 1;
            m_sri.Save();
            sgri.Save();
            //curentGroupKey = int.Parse(lueSystem_Group.EditValue.ToString());

            object[] obj = new object[] {  
                this.m_sri.Properties.System_RuleKey,
                m_sri.Properties.RuleID,
                m_sri.Properties.RuleName,
                m_sri.Properties.RuleNameVi,
                m_sri.Properties.Description,

               sgri.Properties.AllowAccess,
               sgri.Properties.AllowAdd,
               sgri.Properties.AllowDelete, 
               sgri.Properties.AllowEdit,
               sgri.Properties.AllowExport,
               sgri.Properties.AllowImport,
               sgri.Properties.AllowPrint,
               sgri.Properties.AllowSave,
               new System_GroupInfo(int.Parse(lueSystem_Group.EditValue.ToString())).Properties.Name
            };
            passData(obj);
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}